﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace sqxt
{
    public partial class remind : Form
    {
        public remind()
        {
            InitializeComponent();
        }
        bool size = false;
        int pageSize = 0;     //每页显示行数                       
        int nMax = 0;         //总记录数                           
        int pageCount = 0;    //页数＝总记录数/每页显示行数        
        int pageCurrent = 0;   //当前页号                          
        int nCurrent = 0;      //当前记录行                        
        DataSet ds = new DataSet();
        DataTable dtInfo = new DataTable();
        SqlConnection conn = new SqlConnection(Dbconnect.s1);
        private void remind_Load(object sender, EventArgs e)
        {
            label1.Visible = false;
            labelX5.Visible = false;
            dateTimePicker2.Visible = false;
            comboBox1.Items.Add("健康体检");
            comboBox1.Items.Add("高血压随访");
            comboBox1.Items.Add("糖尿病随访");
            comboBox1.Items.Add("孕产妇随访");
            comboBox1.Items.Add("老年人随访");
            comboBox1.Items.Add("残疾人随访");
            comboBox1.Items.Add("儿童随访");
            ArrayList arr = new ArrayList();
            SqlCommand com = new SqlCommand("select MedSite_name from medsite ", conn);
            conn.Open();
            SqlDataReader sdr = com.ExecuteReader();
            while (sdr.Read())
            {

                arr.Add(sdr.GetString(0));
            }
            foreach (string s in arr)
            {
                comboBox2.Items.Add(s);
            }
            sdr.Close();
            conn.Close();
            ArrayList arr1 = new ArrayList();
            SqlCommand com1 = new SqlCommand("select empcode from employee ", conn);
            conn.Open();
            SqlDataReader sdr1 = com1.ExecuteReader();
            while (sdr1.Read())
            {

                arr1.Add(sdr1.GetString(0));
            }
            foreach (string s1 in arr1)
            {
                comboBox3.Items.Add(s1);
            }
            sdr.Close();
            conn.Close();
            //panel1.Height = 26;
            //panel1.Width = listView1.Width;
            //groupBox2.Height = listView1.Height - 125;
            size = true;
            conn.Open();
            find("  and  next_followup_date = '"+DateTime.Now.Date.ToShortDateString() + "'");
            this.buttonX1_Click(sender,e);

        }

        public void find(string tj)
        {
            try
            {
                dgvInfo.DataSource = ds.Tables["myds1_talble"];
                //conn.Open();
                string strSql = "SELECT  followup_id,person_name  姓名, person_sex 性别 ,person_healthfile_no 档案编号, next_followup_date 随访时间, person_birthdate 出生日期, person_idtype 证件类型, person_id_no 证件号码, person_family_tel 家庭电话, person_addr  家庭地址,followup_class 随访类型, followup_doctor 责任医生 from  vm_remind  where  person_healthfile_no <>''  and real_followup_date is null  and person_isend='未归档'  " + tj; 
                SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
                sda.Fill(ds, "ds");
                conn.Close();
                dtInfo = ds.Tables[0];
                InitDataSet();
            }
            catch (Exception e1)
            {
                
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        private void InitDataSet()
        {
            pageSize = 50;      //设置页面行数              
            nMax = dtInfo.Rows.Count;
            pageCount = (nMax / pageSize);    //计算出总页数    
            if ((nMax % pageSize) > 0) pageCount++;
            pageCurrent = 1;    //当前页数从1开始           
            nCurrent = 0;       //当前记录数从0开始         
            LoadData();
        }
        private void LoadData()
        {
            while (this.dgvInfo.Rows.Count != 0)
            {
                this.dgvInfo.Rows.RemoveAt(0);
            }
           
            int nStartPos = 0;   //当前页面开始记录行                       
            int nEndPos = 0;     //当前页面结束记录行                       
            DataTable dtTemp;
            
            dtTemp= dtInfo.Clone();   //克隆DataTable结构框架    
            
            if (pageCurrent == pageCount)
            {
                nEndPos = nMax;
            }
            else
            {
                nEndPos = pageSize * pageCurrent;
            }
            nStartPos = nCurrent;
            toolStripLabel1.Text = "共有" + nMax.ToString() + "条记录 每页50行";
            toolStripTextBox1.Text = Convert.ToString(pageCurrent);
            if (nMax != 0)
            {
                //从元数据源复制记录行                                         
                for (int i = nStartPos; i < nEndPos; i++)
                {
                    dtTemp.ImportRow(dtInfo.Rows[i]);
                    nCurrent++;
                }
            }
            bdsInfo.DataSource = dtTemp;
            bdnInfo.BindingSource = bdsInfo;
            dgvInfo.DataSource = bdsInfo;
            dgvInfo.Columns[0].Visible = false;

        }


        private void remind_Resize(object sender, EventArgs e)
        {
            if (!size)
            {
          
                groupBox2.Height = listView1.Height - 125;
            }
            else
            {
                Mainform ma = new Mainform();
              
                groupBox2.Height = ma.Height - 215;
                size = false;

            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {

            dtInfo.Rows.Clear();
            dgvInfo.DataSource = dtInfo;
           

            string cb1="";
            if (comboBox1.Text == "健康体检") { cb1 = " and followup_class='健康体检'"; }
            if (comboBox1.Text == "高血压随访") { cb1 = "  and  followup_class  like '%高血压%'"; }
            if (comboBox1.Text == "糖尿病随访") { cb1 = "  and followup_class  like '%糖尿病%'"; }
            if (comboBox1.Text == "老年人随访") { cb1 = "  and followup_class='老年人随访'"; }
            if (comboBox1.Text == "孕产妇随访") { cb1 = "  and followup_class='孕产妇随访'"; }
            if (comboBox1.Text == "残疾人随访") { cb1 = "  and followup_class='残疾人随访'"; }
            if (comboBox1.Text == "儿童随访") { cb1 = "  and followup_class  like '%儿童%'"; }
            if (comboBox1.Text.Trim() == "") { cb1 = ""; }
            string cb2 = "";
            if (comboBox2.Text.Trim() != "") { cb2 = "  and  Person_Medside='" + comboBox2.Text + "'"; }
            string cb3 = "";
            if (comboBox3.Text.Trim() != "") { cb3 = "  and  create_doctor='"+comboBox3.Text+"'"; }
            string stime = "  and  next_followup_date = '" + DateTime.Now.Date.ToShortDateString() + "'";
            if (checkBox1.Checked) { stime = "  and  next_followup_date='" + DateTime.Parse(dateTimePicker1.Text) + "'"; }
            if (checkBox3.Checked) { stime = "   and  next_followup_date between '" + DateTime.Parse(dateTimePicker1.Text) + "'  and  '" +DateTime.Parse( dateTimePicker2.Text) + "'"; }
            string sql = cb1+"  " + cb2 + "  "+cb3 +"  "+ stime;
            find(sql); //调用 查出数据的方法
            dgvInfo.Refresh();//显示数据页面刷新
           
                conn.Open();
                string yj = "select distinct(person_name) from vm_remind  where  person_healthfile_no <>''  and real_followup_date is null  and person_isend='未归档' and next_followup_date<'" +DateTime.Now.ToShortDateString()+ "'";
                SqlCommand com = new SqlCommand(yj, conn);
                SqlDataReader sdr = com.ExecuteReader(); 
                string name = "";
                while (sdr.Read())
                {
                    name = name +" "+sdr[0].ToString();

                }
                conn.Close();
                if (name != "")
                {
                    name = "居民 " + name + "  过了计划日期但尚未随访";
                    remine_num rn = new remine_num(name,20);
                    rn.Show();
                }
             
        }


        private void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "上一页")
            {
                pageCurrent--;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页，请点击“下一页”查看！");
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                pageCurrent++;
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页，请点击“上一页”查看！");
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                label1.Visible = true;
                labelX5.Visible = true;
                dateTimePicker2.Visible = true;
                checkBox1.Checked = false;
            }
          
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                label1.Visible = false;
                labelX5.Visible = false;
                dateTimePicker2.Visible = false;
                checkBox3.Checked = false;
            }
           
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (nMax != 0)
            {
                string id = dgvInfo.Rows[dgvInfo.CurrentRow.Index].Cells[0].Value.ToString();
                string riqi = dgvInfo.Rows[dgvInfo.CurrentRow.Index].Cells[4].Value.ToString();
                remind_rework rr = new remind_rework(id, riqi);
                rr.ShowDialog();
            }

        }

    

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            
            string savePath = "";
            saveFileDialog1.Filter = "EXCEL(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                savePath = saveFileDialog1.FileName;
                expExcel.ExportData(dgvInfo, savePath);
            }

        }

      
    }
}